package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.dto.StockOutDeliveryDto;
import com.dingreading.cloud.admin.entity.WhStockOut;
import com.dingreading.cloud.admin.entity.table.WhStockOutTableDef;
import com.dingreading.cloud.admin.mapper.WhStockOutMapper;
import com.dingreading.cloud.admin.service.WhStockOutService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class WhStockOutServiceImpl extends BaseServiceImpl<WhStockOutMapper, WhStockOut, WhStockOutTableDef> implements WhStockOutService {

    @Override
    protected WhStockOutTableDef getTable() {
        return WhStockOutTableDef.whStockOut;
    }

    @Override
    public Page<WhStockOut> pageList(PageUtil pageUtil, String keyword, Integer status, Integer stockOutMethod) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.orderCode.like(keyword)
                    .or(table.outCode.like(keyword))
            );

        if (null != status)
            condition.and(table.status.eq(status));
        if (null != stockOutMethod)
            condition.and(table.stockOutMethod.eq(stockOutMethod));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public WhStockOut getByOutCode(String outCode) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.outCode.eq(outCode));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public List<StockOutDeliveryDto> deliveryListByCodes(List<String> outCodes) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.expressName, table.expressCode, table.expressPrice, table.expressPayType, table.expressTime,
                        table.deliveryWarehouseId, table.deliveryWarehouseName, table.status, table.outCode)
                .from(table)
                .where(table.outCode.in(outCodes));
        return getMapper().selectListByQueryAs(wrapper, StockOutDeliveryDto.class);
    }

    @Override
    public boolean ok(Long id) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.eq(id))
                .and(table.status.eq(2));

        return UpdateChain.of(WhStockOut.class)
                .set(table.status, 1)
                .set(table.statusName, "出库完成")
                .where(condition)
                .update();
    }

    @Override
    public boolean cancel(String outCode, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.outCode.eq(outCode))
                .and(table.status.eq(2));

        return UpdateChain.of(WhStockOut.class)
                .set(table.status, 0)
                .set(table.statusName, "已取消")
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public List<WhStockOut> getByOrderCodeAndStockOutMethod(String orderCode, Integer stockOutMethod) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderCode.eq(orderCode))
                .and(table.stockOutMethod.eq(stockOutMethod));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public List<WhStockOut> getByOrderCodesAndStockOutMethod(List<String> orderCode, Integer stockOutMethod) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderCode.in(orderCode));
        if (null != stockOutMethod)
            condition.and(table.stockOutMethod.eq(stockOutMethod));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public long getSaveOutCnt(String orderCode) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderCode.eq(orderCode))
                .and(table.status.eq(2));
        return getMapper().selectCountByCondition(condition);
    }

    @Override
    public long getOkOutCnt(String orderCode) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderCode.eq(orderCode))
                .and(table.status.eq(1));
        return getMapper().selectCountByCondition(condition);
    }

    @Override
    public int dropByOutCode(String outCode) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.outCode.eq(outCode))
                .and(table.status.eq(2));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public boolean updateGoodsTypeNumAndNum(String outCode, Integer goodsTypeNum, Integer num) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.outCode.eq(outCode))
                .and(table.status.eq(2));

        return UpdateChain.of(WhStockOut.class)
                .set(table.goodsTypeNum, goodsTypeNum)
                .set(table.num, num)
                .where(condition)
                .update();
    }

    @Override
    public List<WhStockOut> getByOrderCodeV2(String orderCode) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderCode.eq(orderCode))
                .and(table.status.eq(1))
                .and(table.stockOutMethod.in(1, 3));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public int dropSaveOuts(String orderCode) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderCode.eq(orderCode))
                .and(table.status.eq(2));
        return getMapper().deleteByCondition(condition);
    }

}
